package com.infolab.aims.service;

import java.util.Date;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infolab.aims.common.PaginationHelper;
import com.infolab.aims.common.QueryHelper;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.domain.Section;
import com.infolab.aims.service.intf.SectionService;


@Service
@Transactional
public class SectionServiceImpl implements SectionService {
	
	Log logger = LogFactory.getLog(getClass());
	@PersistenceContext
	private EntityManager em;
	
	public void createSection(Section section)
	{
		section.setActive(true);
		
		Section newSection = new Section();
		
		section.setModifiedBy(section.getCreatedBy());
		section.setDateCreated(new Date());
		section.setDateModified(new Date());
		
		BeanUtils.copyProperties(section,newSection);
		
		em.persist(newSection);
		
		section.setSectionId(newSection.getSectionId());
		
	}

	public void updateSection(Section section) {
		
		if(section.getDepartment()==null || section.getDepartment().getDepartmentId()==null)
			section.setDepartment(null);
		
		Section newSection = em.find(Section.class, section.getSectionId());
		
		section.setDateModified(new Date());
		BeanUtils.copyProperties(section,newSection);
		
		em.merge(newSection);
	}


	public Section getSection(Integer sectionId) {
		
		return em.find(Section.class, sectionId);
	}

	public Section findSection(Map<String, Object> searchCriteria) {
		
		return null;
	}

	public SearchResult searchSection(
			SearchCriteria searchCriteria) {

		if(searchCriteria.getCurPage()==null)
			searchCriteria.setCurPage(1);
		
		SectionPaginationHelper helper = new SectionPaginationHelper();
		
		return helper.getSearchResultFromQueryHelper(em, searchCriteria);
	}
	
	public void deleteSection(Integer sectionId) {
		
		Query query = em.createQuery("update Section set active=0 where sectionId=:sectionId");
		
		query.setParameter("sectionId", sectionId);
		
		query.executeUpdate();
		
	}
	
	public void deleteSection(Section section) {
		section.setDateModified(new Date());
		section.setActive(false);
		em.merge(section);
	}
	
	public static class SectionPaginationHelper extends PaginationHelper<Section> {

		@Override
		public String[] getValidColumnName() {
			return new String[]{"sectionCode","sectionName","active"};
		}
		
		@Override
		public String[] getValidSortName() {
			return new String[]{"sectionCode","sectionName","department.departmentName","modifiedBy","dateModified"};
		}

		@Override
		public String getDefaultSortName() {
			return "sectionName";
		}
		
		@Override
		public QueryHelper getQueryHelper() {
			QueryHelper queryHelper = new QueryHelper();
			queryHelper.select("select section from Section section");
			return queryHelper;
		}
	}
}
